package com.aaa.dang.modules.user.service.impl;

import com.aaa.dang.common.entity.Room;
import com.aaa.dang.common.systemconst.ISystemConst;
import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.user.dao.IRoomChangeDao;
import com.aaa.dang.modules.user.service.IRoomChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JinChaoLiang
 * @calss QY87
 * @description 描述
 * @date 2018/12/17
 */
@Service
public class RoomChangeServiceImpl implements IRoomChangeService {

    @Autowired
    private IRoomChangeDao changeDao;
    @Override
    public Map<String,Object> getInfoByUsername(String username) {
        Map<String,Object> map = changeDao.getInfoByUsername(username);
        System.out.println(map);
        return map;
    }

    /**
     * 进行user_id的互换
     * 1 根据房间用户中间表得到床位id
     * 2 根据床位id分别向两个表中修改user_id
     * @param id
     * @param id1
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    @SysLog(operateType = SysLog.Type.UPDATE,desc = "两人互换床位")
    public int updateRoom(int id, int id1) {
        int bedOne = changeDao.getBedOneId(id);
        int bedTwo = changeDao.getBedTwoId(id1);
        Map<String,Object>map = new HashMap<>();
        int i=ISystemConst.FAULT_CODE;
        map.put("id",id);
        map.put("id1",id1);
        map.put("bedOne",bedOne);
        map.put("bedTwo",bedTwo);
        try {
            changeDao.updateOne(map);
            changeDao.updateTwo(map);
            i=ISystemConst.SUCCESS_CODE;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return i;
    }

    @Override
    public ResultModel getRoomByBuildId(Map<String,Object> map) {

        ResultModel rm = new ResultModel();
        rm.setData(changeDao.getRoomByBuildId(map));
        return rm;
    }

    @Override
    public ResultModel getBedByBuildId(int roomid) {
        ResultModel rm = new ResultModel();
        rm.setData(changeDao.getBedByBuildId(roomid));
        return rm;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @SysLog(operateType = SysLog.Type.UPDATE,desc = "换空床位")
    public int updateEmptyRoom(Map<String, Object> map) {
        int i=ISystemConst.FAULT_CODE;
        try {
            changeDao.updateUseridToEmpty(map);
            changeDao.updateUseridToEmptyRoom(map);
            i=ISystemConst.SUCCESS_CODE;
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return i;
    }
}
